package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDemo10 {
    public static void main(String[] args) {
        try(Connection connection = DBUtil.getConnection()) {
            connection.setAutoCommit(false);
            String sql = "DELETE FROM user WHERE id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1,5);
            int num = ps.executeUpdate();
            System.out.println(num>0? "删除成功":"删除失败");

            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入rollback(回滚)或commit(提交)");
            String s = scanner.nextLine();
            switch (s){
                case "rollback":
                    connection.rollback();
                    System.out.println("回滚了,删除不作数");
                    break;
                case "commit":
                    connection.commit();
                    System.out.println("提交了,删除成功");
                    break;
                default:
                    System.out.println("输入有误");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
